package dfl.bysj.service.impl;

import dfl.bysj.config.RedisKey;
import dfl.bysj.entity.Academy;
import dfl.bysj.entity.Grade;
import dfl.bysj.mapper.GradeMapper;
import dfl.bysj.service.AcademyService;
import dfl.bysj.service.GradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DFL
 * @since 2022-04-26
 */
@Slf4j
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {

    private RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private List<Grade> grades;

    private AtomicReference<Grade> grade= new AtomicReference<>(new Grade());

    private AcademyService academyService;

    @Autowired
    public void setAcademyService(AcademyService academyService) {
        this.academyService = academyService;
    }

    @Override
    public Grade getGradeBygraName(String graName) {
        getGrades();
        grades.stream()
                .filter(s-> Objects.equals(s.getGraName(), graName))
                .forEach(s-> grade.set(s));
        if (grade.get().getGraId()==null){
            return null;
        }
        return grade.get();
    }

    @Override
    public Grade getGradeById(Integer graId) {
        getGrades();
        grades.stream()
                .filter(s-> Objects.equals(s.getGraId(), graId))
                .forEach(s-> grade.set(s));
        if (grade.get().getGraId()==null){
            return null;
        }
        return grade.get();
    }

    @Override
    public List<Grade> getALLGrade() {
         getGrades();
         return grades;
    }

    @Override
    public List<Grade> getGradesByAcaId(Integer acaId) {
        log.info(String.valueOf(acaId));

        getGrades();
        log.info(grades.toString());

        List<Grade> gradesByAcaId=new ArrayList<>();
        grades.stream()
                .filter(s->s.getAcaId()==acaId)
                .forEach(gradesByAcaId::add);

        log.info(gradesByAcaId.toString());
        return gradesByAcaId;
    }

    @Override
    public List<Grade> getALLGradeByAcaName(String acaName) {
      Academy academy= academyService.getAcademyByacaName(acaName);
        return getGradesByAcaId(academy.getAcaId());
    }

    void getGrades(){
        grades= (List<Grade>) redisTemplate.opsForValue().get(RedisKey.ALL_GRADE);
        if (grades==null){
            updateRedisOfGrades();

        }
    }

    private void updateRedisOfGrades(){
        grades=list();
        redisTemplate.opsForValue().set(RedisKey.ALL_GRADE,grades,7, TimeUnit.DAYS);
    }
}
